package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.LinkAdapter;
import flc.ast.databinding.ActivityPlayLinkBinding;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.ToastUtil;

public class PlayLinkActivity extends BaseAc<ActivityPlayLinkBinding> {

    private List<String> listShow=new ArrayList<>();
    private LinkAdapter linkAdapter;
    private Dialog myClearDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_play_link;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);
        mDataBinding.ivPlayLinkBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        linkAdapter=new LinkAdapter();
        mDataBinding.rvPlayLinkList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        mDataBinding.rvPlayLinkList.setAdapter(linkAdapter);
        mDataBinding.ivSearchLink.setOnClickListener(this);
        mDataBinding.tvClearRecord.setOnClickListener(this);
        linkAdapter.setOnItemClickListener(this);
        clearDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivSearchLink:
                String videoPath=mDataBinding.etVideoLink.getText().toString();
                if(TextUtils.isEmpty(videoPath)){
                    ToastUtils.showShort(getString(R.string.please_input_play_uri));
                }else{
                    saveRecord(videoPath);
                    PlayActivity.videoUri=videoPath;
                    startActivity(PlayActivity.class);
                }
                break;
            case R.id.tvClearRecord:
                myClearDialog.show();
                break;
            case R.id.ivDialogClearCancel:
                myClearDialog.dismiss();
                break;
            case R.id.ivDialogClearConfirm:
                if (listShow.size()==0){
                    ToastUtils.showShort(getString(R.string.null_play));
                    return;
                }
                listShow.clear();
                SPUtil.putObject(mContext, listShow, new TypeToken<List<String>>() {
                }.getType());
                getData();
                myClearDialog.dismiss();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        saveRecord(linkAdapter.getItem(position));
        PlayActivity.videoUri=linkAdapter.getItem(position);
        startActivity(PlayActivity.class);
    }

    @Override
    protected void initData() {

    }

    private void getData(){
        listShow.clear();
        List<String> listSp = SPUtil.getObject(mContext, new TypeToken<List<String>>() {
        }.getType());
        if (listSp != null && listSp.size() > 0) {
            Collections.reverse(listSp);
            listShow.addAll(listSp);
//            if(listSp.size()>3){
//                listShow=listSp.subList(0, 3);
//            }
            linkAdapter.setList(listShow);
            mDataBinding.rvPlayLinkList.setVisibility(View.VISIBLE);
            mDataBinding.llPlayLinkNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvPlayLinkList.setVisibility(View.GONE);
            mDataBinding.llPlayLinkNoData.setVisibility(View.VISIBLE);
        }
    }

    private void saveRecord(String videoPath) {
        List<String> listSave = new ArrayList<>();
        listSave.add(videoPath);
        List<String> listSp = SPUtil.getObject(mContext, new TypeToken<List<String>>() {
        }.getType());
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            SPUtil.putObject(mContext, listSp, new TypeToken<List<String>>() {
            }.getType());
        } else {
            SPUtil.putObject(mContext, listSave, new TypeToken<List<String>>() {
            }.getType());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    //删除弹框
    private void clearDialog() {
        myClearDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_clear, null);
        myClearDialog.setContentView(inflate);
        myClearDialog.setCancelable(true);
        //获取当前Activity所在的窗体
        Window dialogWindow = myClearDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogConfirm = inflate.findViewById(R.id.ivDialogClearConfirm);
        ImageView dialogCancel= inflate.findViewById(R.id.ivDialogClearCancel);
        dialogConfirm.setOnClickListener(this);
        dialogCancel.setOnClickListener(this);
    }

}